package com.chrray.position.util;

import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author LM
 * @date 2023/11/23 17:53
 */
public class HighTimeUtil {

    private static final int BIT_OFFS_SCORE = 0;
    private static final int BIT_SIZE_SCORE = 14;
    private static final int BIT_OFFS_TIME = BIT_OFFS_SCORE + BIT_SIZE_SCORE;
    private static final int BIT_SIZE_TIME = 14;
    private static final int BIT_OFFS_LEVEL = BIT_OFFS_TIME + BIT_SIZE_TIME;
    private static final int BIT_SIZE_LEVEL = 3;


    public static Integer getOnTime(int n) {

        return (n >> BIT_OFFS_TIME) & ((1 << BIT_SIZE_TIME) - 1);
    }

    public static Integer getLevel(int n) {

        return (n >> BIT_OFFS_LEVEL) & ((1 << BIT_SIZE_LEVEL) - 1);
    }

    public static Integer getScore(int n) {

        return (n >> BIT_OFFS_SCORE) & ((1 << BIT_SIZE_SCORE) - 1);
    }


    /**
     *
     * @param list  时间集合 ms
     * @param num  时间长度  ms
     * @return
     */
    public static Integer getStartTime(List<Integer> list,Integer num) {
        if(CollectionUtils.isEmpty(list)){
            return 0;
        }
        Integer  startTime=0;
        Integer  totalNum=0;
        for (int i = 0; i < list.size(); i++) {

            for (int j = i; j <list.size(); j++) {
                if(list.get(j)-(list.get(i)+num)>0){
                    if(totalNum<=(j-i)){
                        totalNum=(j-i);
                        startTime=list.get(i);
                        break;
                    }
                    break;
                }

            }
        }
        return startTime;
    }

    public static String getDoubleString(Integer num) {
        int i = num % 10;
        int sec = num / 10;
        if(i>0){
            return sec+"."+i;
        }else {
            return  sec+"";
        }
    }

}
